<?php
class Model@model_name@ extends Model {
	public function add@module_name@($data) {
      	$this->db->query("INSERT INTO `" . DB_PREFIX . "@module_table_name@` SET " .
      			@input_fields@      			 
				"'");

      	$@module_primary_key@ = $this->db->getLastId();
      	
      	return $@module_primary_key@;
	}
	
	public function edit@module_name@($@module_primary_key@, $data) {
		$this->db->query("UPDATE `" . DB_PREFIX . "@module_table_name@` SET " .
				@update_fields@				 
				"' WHERE `@module_primary_key@` = '" . (int)$@module_primary_key@ . "'");		
	}
	
	public function delete@module_name@($@module_primary_key@) {
      	$this->db->query("DELETE FROM `" . DB_PREFIX . "@module_table_name@` WHERE `@module_primary_key@` = '" . (int)$@module_primary_key@ . "'");
	}
	
	public function get@module_name@($@module_primary_key@) {
		
      	$query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "@module_table_name@` WHERE `@module_primary_key@` = '" . (int)$@module_primary_key@ . "'");
		
		return $query->row;
	}
	
	public function get@module_name@s($data = array()) {
		$sql = "SELECT `@module_primary_key@`, @list_fields@ FROM `" . DB_PREFIX . "@module_table_name@` WHERE 1=1 ";
		
		@filter_db_fields@
		
		$sort_data = array(
			@sort_fields@
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY `@module_primary_key@`";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}		
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getTotal@module_name@s($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "@module_table_name@` WHERE 1=1 ";
		
		@filter_db_fields@
		
      	$query = $this->db->query($sql);
      	
		return $query->row['total'];
	}
	
	@new_methods@			
}
?>